<?php
namespace app\api\controller;

use think\Controller;
use think\Request;
use app\model\VisiterModel;
use app\model\UserModel;
use app\model\DeptModel;
use think\Db;
use app\common\BaseController;

class Visiter extends BaseController
{
    private $table = 'vister';
    private $table_view_app_meeting_info = 'view_new_app_meeting_info';
    private $table_view = 'view_vister';
    public function _initialize(){
        // $this->request = Request::instance();
        $this->noNeedLogin = [];
    }

    public function add_post(){
        $uid = trim($this->request->param('uid'));
        $companyid = trim($this->request->param('companyid'));
        $deptid = trim($this->request->param('deptid'));
        $starttime = trim($this->request->param('starttime'));
        $endtime = trim($this->request->param('endtime'));

        if(empty($uid)){
            return toJson(['message'=>'用户ID不能为空','code'=>'400']);
        }

        if(empty($companyid)){
            return toJson(['message'=>'公司ID不能为空','code'=>'400']);
        }

        if(empty($deptid)){
            return toJson(['message'=>'部门ID不能为空','code'=>'400']);
        }

        if(empty($starttime) || empty($endtime)){
            return toJson(['message'=>'请选择有效时间','code'=>'400']);
        }


        $existDept = DeptModel::where('id','eq',$deptid)->where('companyid','eq',$companyid)->find();
        if(empty($existDept)){
            return toJson(['message'=>'公司部门不存在','code'=>'400']);
        }

        $existUser = UserModel::where('id','eq',$uid)->find();
        if(empty($existUser)){
            return toJson(['message'=>'用户不存在','code'=>'400']);
        }

        // $visiterData = VisiterModel::where('uid','eq',$uid)->where('companyid','eq',$companyid)->where('deptid','eq',$deptid)->find();
         $visiterData = Db::table($this->table_view)->where('uid = '.$uid.' and companyid ='.$companyid.' and deptid ='.$deptid.' and deletetime is null')->find();
         $property_check = $this->find('property_relation','companyid = '.$companyid);

        if($visiterData){
            $where = 'uid = '.$uid.' and companyid = '.$companyid.' and deptid = '.$deptid;
            $data = [
                'starttime' => $starttime,
                'endtime' => $endtime
            ];
            $updateres =$this->update('visiter',$where,$data);
            if($property_check){
                $where = 'uid = '.$uid.' and companyid = '.$companyid;
                $updateres_two = $this->update('visiter',$where,$data);
            }
            
            return toJson(['message'=>'设定长期受访人成功','code'=>'200']);
        }
        if($property_check){
             $data = [
                'uid' => $uid,
                'companyid' => $property_check['property_id'],
                'starttime' => $starttime,
                'endtime' => $endtime,
                'createtime' => time()

             ];
             $res = $this->insert('property_relation',$data);
            }
        $visiterModel = new VisiterModel;
        $visiterModel->uid = $uid;
        $visiterModel->companyid = $companyid;
        $visiterModel->deptid = $deptid;
        $visiterModel->starttime = $starttime;
        $visiterModel->endtime = $endtime;
        $visiterModel->createtime = time();
        $visiterModel->save();

        if( $visiterModel->id > 0 ){
            return toJson(['message'=>'新增长期受访人成功','code'=>'200']);

        }else{
            return toJson(['message'=>'新增长期受访人出错，请联系客服！','code'=>'400']);
        }

    }

    public function edit_post(){
        $id = trim($this->request->param('id'));
        $starttime = trim($this->request->param('starttime'));
        $endtime = trim($this->request->param('endtime'));

        if(empty($id)){
            return ['message'=>'用户ID不能为空','code'=>'400'];
        }

        if(empty($starttime) || empty($endtime)){
            return ['message'=>'请选择有效时间','code'=>'400'];
        }


        $visiterData = VisiterModel::where('id','eq',$id)->find();
        if(empty($visiterData)){
            return ['message'=>'该长期受访人不存在','code'=>'400'];
        }


        $visiterData->starttime = $starttime;
        $visiterData->endtime = $endtime;
        $visiterData->save();

        return ['message'=>'修改长期受访人成功','code'=>'200'];

    }
    /**
     * [delete_post description]
     * @HtttpRequest                          |post
     * @Author       
     * @Param        id--visterid companyid-受访公司id  deptid-受访部门id
     * @DateTime     
     * @LastTime     2020-07-29T15:17:26+0800
     * @return       [type]                   [description]
     */
    public function delete_post(){
        $id = trim($this->request->param('id'));
        // $companyid = trim($this->request->param('companyid'));
        // $deptid = trim($this->request->param('deptid'));

        if(empty($id)){
            return ['message'=>'长期受访人ID不能为空','code'=>400];
        }        
        // if(empty($companyid)){
        //     return ['message'=>'受访公司ID不能为空','code'=>400];
        // }

        $visiterData = VisiterModel::where('id','eq',$id)->find();
        if(!$visiterData){
            return ['message'=>'长期受访人不存在','code'=>'400'];
        }
        $where = 'id = '.$id;
        $data= [
            'deletetime' => time()
        ];
        $delres = $this->update('visiter',$where,$data);
        if($delres!=true){return toJson(['message'=>'删除失败','code'=>'400']);}

        return toJson(['message'=>'删除长期受访人成功','code'=>'200']);
    }

    /*长期访客-列表*/
    public function getlist_get(){

        $deptList = Db::table('view_vister')
                        //->where($where)
                        ->where('deletetime is null')
                        //->page($page,$pagesize)
                        ->group('uid')
                        ->select();

        return toJson(['message'=>'获取长期受访人列表成功','code'=>'200','data'=>$deptList]);
        
        $page = trim($this->request->param('page'));
        $pagesize = trim($this->request->param('pagesize'));
        $keyword = trim($this->request->param('keyword'))?:'';

        $page = empty($page) ? 1 : $page;
        $pagesize = empty($pagesize) ? 10 : $pagesize;
        if(!$this->user->companyid){return toJson(['message'=>'未加入公司','code'=>'400']);}
        $where = $keyword ? ":name like %".$keyword."% and companyid =".$this->user->companyid : 'companyid  ='.$this->user->companyid;
        // $where = ' companyid = '.$this->user->companyid.' and appointment_status = 1 ';
        // $deptList = VisiterModel::where($where)->page($page,$pagesize)->select();
        // $deptList = Db::table($this->table_view_app_meeting_info)
        //                 ->where($where)
        //                 ->group('vister_uid')
        //                 ->order('app_visittime desc')
        //                 ->page($page,$pagesize)
        //                 ->select();
        $deptList = Db::table('view_vister')
                        //->where($where)
                        ->where('deletetime is null')
                        //->page($page,$pagesize)
                        ->group('uid')
                        ->select();

        return toJson(['message'=>'获取长期受访人列表成功','code'=>'200','data'=>$deptList]);


    }

    /*来访记录-列表*/
    public function applogslist_get(){
    $offset = trim($this->request->param('offset'));
    $length = trim($this->request->param('length'));
    // $keyword = trim($this->request->param('keyword'));
    $page = empty($page) ? 1 : $page;
    $pagesize = empty($pagesize) ? 10 : $pagesize;

    switch ($this->user['roleid']) {
    case 0:
        # code...
        $where = 'vister_uid = '.$this->uid.' and appointment_status =1';
        break;
    case 1:
    case 2:
        $where = 'companyid = '.$this->user['companyid'].' and arrive_status !=0';
        break;
    case 5:
    case 6:
    case 8:
        $check_company_info = $this->find('view_company','id = '.$this->user['companyid']);
        $where = 'companyid = '.$this->user['companyid'].' and arrive_status !=0';
        if($check_company_info['type'] == 2){
            $companyid_list = $this->selectView('view_company','property_id = '.$this->user['companyid'],'',0,9999);
            $companyid_arr = array_column($companyid_list, 'id');
            if(count($companyid_arr)>0){
                $companyid_str = implode(",", $companyid_arr);
            }else{
                $companyid_str = '0';
            }
            $where = 'companyid in ('.$companyid_str.') and arrive_status !=0';
        }
        if($check_company_info['type']==3){
           $where = 'companyid = '.$this->user['companyid'].' and arrive_status !=0';
        }
        
        break;
    case 3:
        $where = 'companyid = '.$this->user['companyid'].' and deptid = '.$this->user['deptid'].' and arrive_status !=0';
    case 4:
        $where = 'companyid = '.$this->user['companyid'].' and app_uid = '.$this->uid.' and arrive_status !=0';
        break;
    default:
        return toJson(['message'=>'访客记录列表-Faild-无权限','code'=>'400']);
        break;
    }
    $order = 'app_visittime desc';
    // $where = $where.' and type = 1 ';
    $where = $where.' and vister_uid is not null';
    // $where = $keyword ? ":name like %".$keyword."%" : '1=1';
    
    // $deptList = VisiterModel::where($where)->page($page,$pagesize)->select();
    $where = $where.' and deletetime is null';
    $data = Db::table($this->table_view_app_meeting_info)->where($where)->select();
    $data = $this->selectView($this->table_view_app_meeting_info, $where, $field = '*', $offset, $length, $order);
    $total = $this->totalView($this->table_view_app_meeting_info, $where, $field = '*');
    return toJson(['message'=>'访客记录列表-Success','code'=>'200','data'=>$data,'count'=>$total]);
    }



}
